<style type="text/css">
    table {
        max-width: 750px;
    }

    .content-textbox {
        border: 1px dashed black;
        max-height: 20em;
        overflow: auto;
    }

    .content-title {
        border: 1px dashed black;
    }
</style>
<div id="berita-dialog" title="Audit Berita" style="display: none;">
    <input type="hidden" class="id" />
    <h1 class="judul title content-title">Judul Berita</h1>
    <p>
        <span class="keterangan"></span>
    </p>
    <p class="content-textbox">
        <span class="sum">
        </span>
    </p>
</div>
<div style="min-height: 400px;">
    <table style="margin-top: 1em;">
        <thead>
            <tr>
                <th>Judul Berita</th>
                <th>Penulis</th>
                <th>Status</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody>
            <?php
            if (count($artikelList) > 0) {
                foreach ($artikelList as $berita) {
                    echo "<tr>";
                    echo "<td>" . $berita->content_id->title . "</td>";
                    echo "<td>" . $berita->content_id->poster_id->display_name . "</td>";
                    echo "<td>" . ($berita->is_approved ? "Approved" : "Pending Approval") . "</td>";
                    echo "<td>" . "<a href='javascript:void(0);' onClick='javascript:showDialog(" . $berita->id . ");'>Edit</a>" . "</td>";
                    echo "</tr>";
                }
            } else {
                echo "<tr>";
                echo "<td style='text-align: center;' colspan='4'>" . "(tidak ada entry berita)" . "</td>";
                echo "</tr>";
            }
            ?>
        </tbody>
    </table>
</div>
<script type="text/javascript">
    jQuery(document).ready(function() {
        $("#berita-dialog").dialog({
            modal: true,
            autoOpen: false,
            width: "53em",
            resizeable: false,
            buttons: {
                "Batal": function() {
                    $(this).dialog("close");
                },
                "Simpan": function() {
                    if (confirm("Simpan berita?")) {
                        $.ajax({
                            type: "POST",
                            url: "<?php echo site_url("artikel/updateApproveArtikel"); ?>",
                            async: false,
                            data: {
                                id: $("#berita-dialog .id").val(),
                                title: $("#berita-dialog .judul").text(),
                                text: $("#berita-dialog .content-textbox .sum").html()
                            },
                            beforeSend: function(xhr) {
                            },
                            success: function(result, status, xhr) {
                                window.location = "<?php echo site_url("usercp/editartikel/b"); ?>";
                            },
                            error: function(xhr, status, error) {
                                window.location = "<?php echo site_url("usercp/editartikel/b"); ?>";
                            }
                        });
                    }
                },
                "Hapus": function() {
                    if (confirm("HAPUS berita?")) {
                        if (confirm("Yakin ingin HAPUS berita?")) {
                            $.ajax({
                                type: "POST",
                                url: "<?php echo site_url("artikel/deleteArtikel"); ?>",
                                async: false,
                                data: {
                                    id: $("#berita-dialog .id").val()
                                },
                                beforeSend: function(xhr) {
                                },
                                success: function(result, status, xhr) {
                                    window.location = "<?php echo site_url("usercp/editartikel/b"); ?>";
                                },
                                error: function(xhr, status, error) {
                                    window.location = "<?php echo site_url("usercp/editartikel/b"); ?>";
                                }
                            });
                        }
                    }
                }
            }
        });

        createTitleEditor("#berita-dialog .judul", function() {
        });
        createContentEditor("#berita-dialog .content-textbox .sum", function() {
        });
    });

    function showDialog(id) {
        $.ajax({
            url: "<?php echo site_url("service/getDataJSON/brtbyid"); ?>",
            method: "POST",
            dataType: "json",
            data: {
                id: id
            },
            success: function(berita) {
                $("#berita-dialog .judul").text(berita.content_id.title);
                $("#berita-dialog .content-textbox .sum").html(berita.content_id.text);
                $("#berita-dialog .id").val(berita.id);
            }
        });

        $("#berita-dialog").dialog("open");
    }
</script>